package com.ruoyi.quartz.service.impl;

import com.ruoyi.quartz.service.EmailService;
import com.ruoyi.quartz.service.MailService;
import com.ruoyi.system.domain.ContentRecords;
import com.ruoyi.system.domain.RecipientRecorda;
import com.ruoyi.system.mapper.ContentRecordsMapper;
import com.ruoyi.system.mapper.RecipientRecordaMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;


/**
 * @author xiaogu
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private RecipientRecordaMapper recipientRecordaMapper;

    @Resource
    private ContentRecordsMapper contentRecordsMapper;

    @Resource
    private MailService mailService;

    @Override
    public void sendEmail(String name) {
        // 根据name从数据库中获取邮箱，内容，主题
        RecipientRecorda recipientRecorda = recipientRecordaMapper.selectRecipientByName(name);

        //根据id从数据库中获取内容
        String contentRecords = getEmailContentForToday(recipientRecorda);

        System.out.println(contentRecords);

        // 发送邮件
        mailService.sendSimpleMail(recipientRecorda.getEmail(), recipientRecorda.getObject(), contentRecords);


    }


    private String getEmailContentForToday(RecipientRecorda recipientRecorda) {
        Calendar calendar = Calendar.getInstance();
        // 获取星期几（1=星期日, 2=星期一, ..., 7=星期六）
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String dayOfWeekStr = Integer.toString(dayOfWeek);

        List<ContentRecords> contentRecords = contentRecordsMapper.selectContent(recipientRecorda.getId(), dayOfWeekStr);

        // 创建 StringBuilder 来拼接 HTML 内容
        StringBuilder contentBuilder = new StringBuilder();

        // 添加 HTML 开头部分
        contentBuilder.append("""
    <div style="border: 2px solid #ADD8E6; padding: 20px; border-radius: 10px; background-color: #F0F8FF; max-width: 600px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);"> \s
        <h4 style="color: #ADD8E6; text-align: center; font-family: 'Lucida Calligraphy', cursive;">
    """)
                .append(contentRecords.isEmpty() ? "小古有趣" : contentRecords.get(0).getTitle())  // 动态插入 email
                .append("""
        </h4> \s
        <div style="display: flex; justify-content: center; align-items: center; margin: 20px 0;"> \s
            <img src="https://xiaogu-web.oss-cn-beijing.aliyuncs.com/d7848244-83d6-4c54-9f34-2c80cba7f264.jpg" alt="Avatar 1" style="width: 50px; height: 50px; border-radius: 50%; margin-right: 20px;"> \s
            <img src="https://pic.imgdb.cn/item/6715fa2bd29ded1a8ccffbf5.gif" alt="GIF" style="width: 50px; height: 50px; margin: 0 20px;"> \s
            <img src="
    """)
                .append(recipientRecorda.getImg())  // 动态插入第二个头像 URL
                .append("""
        " alt="Avatar 2" style="width: 50px; height: 50px; border-radius: 50%; margin-left: 20px;"> \s
        </div> \s
        <hr style="border: 1px solid #ADD8E6;"> \s
        <p style="font-family: 'Lucida Calligraphy', cursive; line-height: 1.6; color: #333;"> \s
    """);

        // 标记是否有有效内容
        boolean hasValidContent = false;

        // 拼接课程信息
        for (int i = 0; i < contentRecords.size(); i++) {
            ContentRecords record = contentRecords.get(i);

            // 如果今日课程、授课老师、上课时间、上课教室全为空，则跳过当前记录
            if (record.getTodayClass() == null && record.getTeacher() == null &&
                    record.getClassTime() == null && record.getClassroom() == null) {
                continue; // 跳过这条记录
            }

            // 标记有有效内容
            hasValidContent = true;

            // 如果没有有效的课程内容，只保留标题部分
            if (hasValidContent) {
                // 拼接课程信息
                contentBuilder.append("<strong style=\"color: #6495ED;\">今日课程")
                        .append(i + 1)
                        .append(":</strong> \s")
                        .append("<span style=\"color: #00CED1; font-weight: bold;\">")
                        .append(record.getTodayClass() != null ? record.getTodayClass() : "未知课程")
                        .append("</span><br> \s")
                        .append("<strong style=\"color: #6495ED;\">授课老师:</strong> \s")
                        .append("<span style=\"color: #00CED1;\">")
                        .append(record.getTeacher() != null ? record.getTeacher() : "未知老师")
                        .append("</span><br> \s")
                        .append("<strong style=\"color: #6495ED;\">上课时间:</strong> \s")
                        .append("<span style=\"color: #00CED1;\">")
                        .append(record.getClassTime() != null ? record.getClassTime() : "未知时间")
                        .append("</span><br> \s")
                        .append("<strong style=\"color: #6495ED;\">上课教室:</strong> \s")
                        .append("<span style=\"color: #00CED1;\">")
                        .append(record.getClassroom() != null ? record.getClassroom() : "未知教室")
                        .append("</span><br><br> \s");
            }
        }


        // 结束 HTML 结构
        contentBuilder.append("</p></div> \s");

        // 返回拼接好的 HTML 字符串
        return contentBuilder.toString();
    }


}
